package com.qianbei.user.person;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.android.volley.Request;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.view.ClearWriteEditText;

public class UpdateNameActivity extends BaseActivity {

    private ClearWriteEditText mEtUpdateName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_name);

        setTitle("更改昵称");
        mTitle.setrightTitle("保存");

        String nickName = getIntent().getStringExtra("nickName");
        if (!TextUtils.isEmpty(nickName)) {
            mEtUpdateName.setText(nickName);
        }
        initView();
    }

    public void initView() {
        mEtUpdateName = (ClearWriteEditText) findViewById(R.id.et_update_name);
    }

    @Override
    public void fClick(int id) {
        super.fClick(id);
        switch (id) {
            case R.id.tv_right:
                String etName = mEtUpdateName.getText().toString();
                if (TextUtils.isEmpty(etName)) {
                    new MsgAlert().show("昵称不能为空");
                } else {
                    initName(etName);
                }

                break;
        }
    }

    private void initName(final String etName) {
        ServerControl sc = new ServerControl(Request.Method.POST, UPDATE_NAME, "uid", SetData.getUserID(), "name", etName);

        sc.serverListener = new ServerControl.ServerListener() {
            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {
                    new MsgAlert().show(sr.bodyData.optString("msg"));
                    SetData.setUserName(etName);
                    Intent intent = new Intent();
                    setResult(232, intent);
                    finish();
                }
            }
        };

        sc.startVolley();

    }
}
